<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserFilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_files', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('file_id')->nullable();
            $table->string('name', '1024')->comment('上传时的文件名');
            $table->string('file_ext' )->nullable()->comment('上传时的文件扩展名');
            $table->string('access_token')->nullable();
            $table->dateTime('access_token_expire_at')->nullable();
            $table->unsignedTinyInteger('status')->default(0)->comment('0正常1已删除');
            $table->unsignedTinyInteger('type')->default(0)->comment('0文件1文件夹2分享链接');
            $table->unsignedInteger('parent_id')->default(0)->comment('记录该文件的直接父级文件夹id');
            $table->unsignedInteger('from_user_id')->nullable()->comment('如果是链接的话，则记录分享者的user_id');
            $table->unsignedTinyInteger('is_public')->default(0)->comment('0私人1公共');
            $table->timestamps();

            $table->index('access_token');
        });
        DB::table('user_files')->insert(
            array(
                array(
                    'id' => 1,
                    'user_id' => 0,
                    'file_id' => '',
                    'name'=> 'public',
                    'file_ext' => '',
                    'access_token' => '',
                    'access_token_expire_at' => '',
                    'status' => 0,
                    'type' => 1,
                    'parent_id' => 0,
                    'from_user_id' => '',
                    'is_public' => 1,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                )
            ));
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_files');
    }
}
